# coding: utf8

                               
db.define_table('language',
    Field('code', 'string', notnull=True, unique=True, length=5, required=True, label=T('Code')),
    Field('description', 'string', notnull=True, required=True, label=T('Language'))
)

class VirtualFieldLanguage():

    def percent(self):
 
        # Compute tot language translate.

        prj_languages=self.language.project_language.select()        
        tot_item=tot_tra=0
        
        for p in prj_languages:
            tot_item+=p.tot_item if p.tot_item else 0
            tot_tra+=p.tra_item if p.tra_item else 0
        
        if tot_item > 0:
            return int((float(tot_tra) / float(tot_item)) * 100)
        
        return 100

db.language.virtualfields.append(VirtualFieldLanguage())

db.define_table('project',
    Field('code', 'string', notnull=True, unique=True, length=10, required=True),
    Field('description', 'string', notnull=True, unique=False, length=50, required=True),
    Field('source_language', db.language, notnull=True, required=True)
)

class VirtualFieldProject():

    def percent(self):
 
        # Compute tot project translate.

        prj_languages=self.project.project_language.select()        
        tot_item=tot_tra=0
        
        for p in prj_languages:
            tot_item+=p.tot_item if p.tot_item else 0
            tot_tra+=p.tra_item if p.tra_item else 0
        
        if tot_item > 0:
            return int((float(tot_tra) / float(tot_item)) * 100)
        
        return 100

db.project.virtualfields.append(VirtualFieldProject())


db.define_table('project_language',
    Field('project', db.project),
    Field('language', db.language),
    Field('tot_item', 'double', default=0, notnull=True),
    Field('tra_item', 'double', default=0, notnull=True)
)

class VirtualFieldProjectLanguage():
    def percent(self):        
        if hasattr(self.project_language, 'tot_item') and self.project_language.tot_item > 0:            
            return int((float(self.project_language.tra_item) / float(self.project_language.tot_item))*100)        
        return 100
        
db.project_language.virtualfields.append(VirtualFieldProjectLanguage())

db.define_table('item',
    Field('key', 'string', length=20, label=T('Key')),
    Field('source', 'text', notnull=True, required=True, label=T('Source Language')),
    Field('target', 'text', label=T('Target Language')),
    Field('comment', 'string'),
    Field('suggestion', 'string'),
    Field('image', 'upload'),
    Field('project_language', db.project_language)
)


languages_all= db().select(db.language.ALL, orderby=db.language.description)
projects_all= db().select(db.project.ALL, orderby=db.project.description)

google_translate_api_key=''

google_translate_api_url='https://www.googleapis.com/language/translate/v2?key=%s&q=%s&source=%s&target=%s'

